package kr.ac.hufs.ice.cap.svacs;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import kr.ac.hufs.ice.cap.network.NetworkData;
import kr.ac.hufs.ice.cap.network.NetworkThread;
import kr.ac.hufs.ice.cap.network.NetworkThread_ChildInfoLoading;
import kr.ac.hufs.ice.cap.network.NetworkThread_FirstChild;
import kr.ac.hufs.ice.cap.network.NetworkThread_ParentInfoLoading;
import kr.ac.hufs.ice.cap.network.NetworkThread_VisitInfoLoading;
import kr.ac.hufs.ice.cap.network.PageStreamer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.FloatMath;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TotalManagement extends Activity { // implements
	// View.OnClickListener
	private final String SERVER_ADDRESS = "http://dislab.iptime.org:2324/parentinfo/check.php";

	Button btn_ChildRegister;
	Button btn_Help;
	Button btn_Visit;
	Button btn_VisitCard;

	String userId;
	String parentPhoneNumber;
	String parentName;

	public boolean change = false;

	StringTokenizer tokens;
	TextView txt_firstchild_GCN;
	TextView txt_firstchild_school;
	TextView txt_secchild_GCN;
	TextView txt_secchild_school;

	NetworkData nd;
	NetworkThread_ParentInfoLoading nt_PIL;
	NetworkThread_ChildInfoLoading nt_CIL;
	NetworkThread_VisitInfoLoading nt_VIL;

	String test;
	// HttpPost httppost;
	// StringBuffer buffer;
	// HttpResponse response;
	// HttpClient httpclient;
	// List<NameValuePair> nameValuePairs;
	String act_LoginKey = null;

	static String PhoneNumber;
	ProgressDialog dialog = null;

	String TAG = TotalManagement.class.getName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.totalmanagement);

		userId = this.getIntent().getStringExtra("UserID");
		parentPhoneNumber = this.getIntent()
				.getStringExtra("ParentPhoneNumber");
		parentName = this.getIntent().getStringExtra("ParentName");
		Log.e("Total_parentPhoneNumber",parentPhoneNumber);
		Log.e("Total_parentName",parentName);

		txt_firstchild_GCN = (TextView) findViewById(R.id.total_firstchild_GCN);
		txt_firstchild_school = (TextView) findViewById(R.id.total_firstchild_school);
		txt_secchild_GCN = (TextView) findViewById(R.id.total_secchild_GCN);
		txt_secchild_school = (TextView) findViewById(R.id.total_secchild_school);

		btn_ChildRegister = (Button) findViewById(R.id.btnchildreg);
		btn_Visit = (Button) findViewById(R.id.btn_visit);
		btn_Help = (Button) findViewById(R.id.btn_help);
		btn_VisitCard = (Button) findViewById(R.id.btn_total_visitcard);
		// Log.e("Error",data.getId());
		//		txt_firstchild_school.setText(parentPhoneNumber);
		//		txt_secchild_school.setText(parentName);

		btn_Visit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(TotalManagement.this,
						VisitApplication.class);
				intent.putExtra("ParentPhoneNum", parentPhoneNumber);
				intent.putExtra("ParentName", parentName);
				Log.e("intent_parentPhoneNumber",parentPhoneNumber);
				Log.e("intent_parentName",parentName);
				startActivity(intent);
			}
		});

		btn_ChildRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(TotalManagement.this,
						ChildRegister.class);
				intent.putExtra("UserID", userId);
				intent.putExtra("ParentPhoneNum", parentPhoneNumber);
				// Log.e("ParentPhoneNum", parentPhoneNumber);
				startActivity(intent);

			}
		});

		btn_Help.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(TotalManagement.this, Help.class));
			}
		});

		btn_VisitCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(TotalManagement.this,
						ParentVisitCard.class);
				intent.putExtra("ParentName", nt_VIL.returnMsgArr[0]);
				intent.putExtra("ParentPhoneNum", nt_VIL.returnMsgArr[1]);
				intent.putExtra("VisitPurpose", nt_VIL.returnMsgArr[2]);
				intent.putExtra("VisitDate", nt_VIL.returnMsgArr[3]);
				startActivity(intent);
			}
		});

		nd = new NetworkData();
		nd.setParentPhoneNumber(parentPhoneNumber);
		nt_CIL = new NetworkThread_ChildInfoLoading(nd, mHandler2);
		nt_CIL.setCommand(NetworkThread_ChildInfoLoading.REGISTER);
		nt_CIL.start();

		nt_VIL = new NetworkThread_VisitInfoLoading(nd, mHandler3);
		nt_VIL.setCommand(NetworkThread_VisitInfoLoading.REGISTER);
		nt_VIL.start();

	}

	//	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
		//		txt_firstchild_GCN.setText("없습니다.");
		//		txt_firstchild_school.setText("자녀 정보가");
		//		txt_secchild_GCN.setText("없습니다.");
		//		txt_secchild_school.setText("자녀 정보가");
		//		txt_firstchild_GCN.setText("2학년 10반 류태준");
		//		txt_firstchild_school.setText("우리초등학교");
		//		txt_secchild_GCN.setText("3학년 1반 이형욱");
		//		txt_secchild_school.setText("토평과학고등학교");


		//---------------복구----------------
		//		Log.e(TAG + mTAG , "onResume start");
		nd = new NetworkData();
		nd.setParentPhoneNumber(parentPhoneNumber);
		new NetworkThread_ChildInfoLoading(nd, mHandler2).setRCommand(NetworkThread_ChildInfoLoading.REGISTER).start();
		//		Log.e(TAG + mTAG , "onResume end");
		//---------------복구----------------

	}

	Handler mHandler2 = new Handler() {
		public void handleMessage(Message msg) {

			if (NetworkThread_ChildInfoLoading.REGISTER == msg.what) {
				// Toast.makeText(context, "등록 완료", Toast.LENGTH_LONG).show();
				String msgContent = msg.obj + "";
				Log.e("메롱", msgContent);
				if (msgContent.equals("Child Load Success")) {

					String GCN = nt_CIL.returnMsgArr[2] + "학년  "
							+ nt_CIL.returnMsgArr[3] + "반  "
							+ nt_CIL.returnMsgArr[4];
					// Log.e("GCN", nt_PIL.returnMsgArr[2]);

					txt_firstchild_school.setText(nt_CIL.returnMsgArr[1]);
					txt_firstchild_GCN.setText(GCN);

					Toast.makeText(TotalManagement.this, "자식 정보 로드 성공입니다.",
							Toast.LENGTH_SHORT).show();
				} else {// fail 한 경우
					Toast.makeText(TotalManagement.this, "자식 정보 로드 실패입니다.",
							Toast.LENGTH_SHORT).show();
				}
			}

		}
	};
	
	Handler mHandler3 = new Handler() {
		public void handleMessage(Message msg) {

			if (NetworkThread_ChildInfoLoading.REGISTER == msg.what) {
				// Toast.makeText(context, "등록 완료", Toast.LENGTH_LONG).show();
				String msgContent = msg.obj + "";
				Log.e("메롱", msgContent);
				if (msgContent.equals("Visit Load Success")) {

					// Log.e("GCN", nt_PIL.returnMsgArr[2]);

					Toast.makeText(TotalManagement.this, "방문 정보 로드 성공입니다.",
							Toast.LENGTH_SHORT).show();
				} else {// fail 한 경우
					Toast.makeText(TotalManagement.this, "방문 정보 로드 실패입니다.",
							Toast.LENGTH_SHORT).show();
				}
			}

		}
	};

}
